 (function() {

     angular.module('BlurAdmin.pages.ConsultingM')
         .controller('newConsultingCtrl', newConsultingCtrl);

     function newConsultingCtrl($scope, $mydialog, $myconfig, $http, $qupload, $ShareData, $log) {
         $scope.is_suer_root = is_suer_root;

         var news_id = $scope.news_id = $ShareData.getNewConsu(); //获取上传过来的id，如果为空则是新增
         //咨询model
         $scope.consulting = {
             title: "",
             imgs: [],
             summary: null,
             content: null,
             top: 4
         }

         //提交新增咨询
         var is_Submit = true;
         $scope.Submit = function() {
             if (is_Submit) {
                 Testing();
             }
         }

         function Testing() { //检测条件是否满足
             var opt = $scope.consulting;
             if (!opt.title) {
                 return $mydialog.fail('请添加标题');
             }
             if (opt.title.lengt > 20) {
                 return $mydialog.fail('标题最多20字');
             }
             if (!opt.summary) {
                 return $mydialog.fail('请添加摘要');
             }
             if (opt.summary.length > 71) {
                 return $mydialog.fail('摘要最多35字');
             }
             if (!opt.content) {
                 return $mydialog.fail('请添加资讯内容');
             }
             if (opt.content.length > 10000) {
                 return $mydialog.fail('资讯内容不能大于10000个字符');
             }
             SendOut();
         }



         function SendOut() { //发送新增/修改咨询请求
             console.log($myconfig.api.base_host);
             var opt = $scope.consulting;
             if (opt.img && opt.img.length == 0) {
                 delete opt.img;
             }
             //$myconfig.api.Consulting_Add_news 新增咨询接口
             //$myconfig.api.Consulting_update_news 修改咨询接口
             var url = news_id ? $myconfig.api.Consulting_update_news : $myconfig.api.Consulting_Add_news;
             news_id && (opt.news_id = news_id);
             is_Submit = false;
             $http.post(url, opt)
                 .then(function(res) {
                     is_Submit = true;
                     console.log(res);
                     if (res.data.success) {
                         $scope.news_list_b(); //刷新咨询列表，父级定义方法；
                         $scope.opt_close(); //关闭弹出窗口
                         return $mydialog.success("设置成功");
                     } else {
                         return $mydialog.fail(res.data.data);
                     }
                 });
         }


         /* 编辑咨询 */
         //获取咨询详情
         $scope.edit = function() {
             $http.get($myconfig.api.Consulting_news_info + "?news_id=" + news_id)
                 .then(function(res) {
                     if (res.data.success) { //成功
                         $scope.consulting = res.data.data;
                     } else {
                         return $mydialog.fail(res.data.data);
                     }
                 });
         }

         // 获取七牛touken
         function qi_t() {
             $http.get($myconfig.api.QiNiu_token).then(function(res) {
                 console.log(res);
                 $scope.qi_token_mun = res.data.token;
                 $scope.qi_key = res.data.key;

             })
         }

         qi_t();
         //七牛图片上传

         $scope.selectFiles = [];
         $scope.context_img_fn = function(val, index) { //富文本 1 ，添加图片2，修改图片3 
             if (!img_type) {
                 return
             }
             $scope.context_img = val;
             if (indxe) {
                 $scope.img_index = index;
             }
         };
         //删除图片
         $scope.img_delete = function(index) {
             $scope.consulting.imgs.splice(index, 1);
         }

         var start = function(index) {
             $scope.selectFiles[index].progress = {
                 p: 0
             };
             $scope.selectFiles[index].upload = $qupload.upload({
                 key: $scope.qi_key,
                 file: $scope.selectFiles[index].file,
                 token: $scope.qi_token_mun
             });
             $scope.selectFiles[index].upload.then(function(response) {
                 // upload success
                 $log.info(response);
                 var url = $myconfig.api.Qiniu_url + response.key;
                 console.log(url);
                 //插入富文本框

                 if ($scope.context_img == 1) {
                     $('#summernote').summernote('insertImage', url);
                 }
                 if ($scope.context_img == 2) {
                     $scope.consulting.imgs.push(url);
                 }

                 if ($scope.context_img == 3) { //替换图片
                     $scope.consulting.imgs.splice($scope.img_index, 1, url);
                 }
                 qi_t();
                 img_type = true;
             }, function(response) {
                 // upload failure
                 $log.info(response);
             }, function(evt) {
                 // progress
                 //evt.totalSize文件大小
                 // evt.loaded已上传多少
                 var mum = Math.floor(100 * evt.loaded / evt.totalSize);
                 //  $scope.context_img == 1 ? ($scope.mystyle.posters.width = mum + '%') : ($scope.mystyle.mainpic.width = mum + '%');
                 $scope.selectFiles[index].progress.p = Math.floor(100 * evt.loaded / evt.totalSize);


             });
         };
         console.log($scope.consulting.imgs.length);
         $scope.abort = function(index) {
             $scope.selectFiles[index].upload.abort();
             $scope.selectFiles.splice(index, 1);
         };
         var img_type = true;
         $scope.img_qu = true;
         $scope.onFileSelect = function($files, type, index) {
             if (!img_type) {
                 return
             }
             $scope.img_qu = true;
             $scope.img_index = index;
             if (type) {
                 $scope.context_img = type;
             }
             if (index) {
                 $scope.img_index = index;
             }

             var offsetx = $scope.selectFiles.length;
             for (var i = 0; i < $files.length; i++) {
                 $scope.selectFiles[i + offsetx] = {
                     file: $files[i]
                 };
                 start(i + offsetx);
             }
         };
         /* 七牛图片上传结束 */

         //富文本编辑器
         var HelloButton = function(context) { //自定义图片上传按钮
             var ui = $.summernote.ui;
             var button = ui.button({
                 contents: '<span  class="glyphicon glyphicon-picture"></span> 图片',
                 tooltip: '图片',
                 click: function() {
                     $scope.context_img = 1; //1富文本 ，2为添加的图片 ,3为替换图片
                     document.getElementById('asd1').click();
                 }
             });

             return button.render(); // return button as jquery object 
         }

         $scope.options = {
             height: 300,
             focus: true,
             airMode: false,
             toolbar: [
                 // ['edit', ['undo', 'redo']],
                 ['headline', ['style']],
                 ['style', ['bold', 'italic', 'underline', 'superscript', 'subscript', 'strikethrough', 'clear']],
                 // ['fontface', ['fontname']],
                 ['textsize', ['fontsize']],
                 ['fontclr', ['color']],
                 ['alignment', ['ul', 'ol', 'paragraph', 'lineheight']],
                 ['height', ['height']],
                 // ['table', ['table']],
                 //['insert', ['link', 'picture']],
                 ['mybutton', ['hello']]
                 // ['view', ['fullscreen', 'codeview']]
                 // ['help', ['help']]
             ],
             buttons: {
                 hello: HelloButton
             }
         };

         news_id && $scope.edit(); //判断是否是编辑,新增的没有值


     }
 })();